package com.comleader.uap.common.util;

import static com.comleader.uap.common.enums.StatDimensionEnum.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.comleader.uap.common.enums.StatDimensionEnum;

import cn.hutool.core.date.DateUtil;

/**
 * @Dscription 统计工具类
 * @Author 连新景
 * @Date 2024/12/04 11:26
 */
public class StatUtil {

    /**
     * @Description 获取统计纬度
     * @Auhtor 连新景
     * @Date 2024/12/04 11:18
     * @param startTime
     * @param endTime
     * @return int
     */
    public static StatDimensionEnum getStatDimension(Date startTime, Date endTime) {
        StatDimensionEnum statDimensionEnum;
        if (DateUtil.year(startTime) == DateUtil.year(endTime)) {
            statDimensionEnum = MONTH;
        } else {
            statDimensionEnum = YEAR;
        }
        if (DateUtil.isSameMonth(startTime, endTime)) {
            statDimensionEnum = DAY;
        }
        if (DateUtil.isSameDay(startTime, endTime)) {
            statDimensionEnum = HOUR;
        }
        return statDimensionEnum;
    }

    /**
     * @Description 获取开始时间和结束时间的区间list,并根据统计纬度，加上单位
     * @Auhtor 连新景
     * @Date 2024/12/04 11:34
     * @param startTime
     * @param endTime
     * @return java.util.List<java.lang.String>
     */
    public static List<String> getStatTimeList(Date startTime, Date endTime) {
        StatDimensionEnum statDimensionEnum = getStatDimension(startTime, endTime);
        String desc = statDimensionEnum.getDesc();
        List<String> list = new ArrayList<>();
        if (statDimensionEnum == HOUR) {
            int startHour = DateUtil.hour(startTime, true);
            int endHour = DateUtil.hour(endTime, true);
            for (int i = startHour; i <= endHour; i++) {
                list.add(i + desc);
            }
        } else if (statDimensionEnum == DAY) {
            int startDay = DateUtil.dayOfMonth(startTime);
            int endDay = DateUtil.dayOfMonth(endTime);
            for (int i = startDay; i <= endDay; i++) {
                list.add(i + desc);
            }
        } else if (statDimensionEnum == MONTH) {
            int startMonth = DateUtil.month(startTime) + 1;
            int endMonth = DateUtil.month(endTime) + 1;
            for (int i = startMonth; i <= endMonth; i++) {
                list.add(i + desc);
            }
        } else if (statDimensionEnum == YEAR) {
            int startYear = DateUtil.year(startTime);
            int endYear = DateUtil.year(endTime);
            for (int i = startYear; i <= endYear; i++) {
                list.add(i + desc);
            }
        }
        return list;
    }

    /**
     * @Description 格式化从数据库中查询出的时间（如小时、天、月经过数据库截取后为两位数(01,02,10,12等)，如果第一个数字为0时需去掉，并加上单位）
     * @Auhtor 连新景
     * @Date 2024/12/04 13:45
     * @param statTime
     * @return java.lang.String
     */
    public static String formatDatabaseTime(String statTime, StatDimensionEnum statDimensionEnum) {
        String formatTime = "";
        if (statTime.startsWith("0")) {
            formatTime = statTime.replace("0", "") + statDimensionEnum.getDesc();
        } else {
            formatTime = statTime + statDimensionEnum.getDesc();
        }
        return formatTime;
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Date startTime = DateUtil.parseDateTime("2024-1-2 11:11:32");
        Date endTime = DateUtil.parseDateTime("2024-10-20 12:11:32");
        int startDay = DateUtil.hour(startTime, true);
        int endDay = DateUtil.hour(endTime, true);
        for (int i = startDay; i <= endDay; i++) {
            list.add(i + "月");
        }
        System.out.println(list);
        System.out.println(DateUtil.beginOfMonth(startTime));
        System.out.println(DateUtil.year(startTime));
    }
}
